<template>
  <div>
    <div>时间标准版:{{ time }}</div>
    <div>年月日:{{ time1 }}</div>
    <div>中文年月日:{{ time2 }}</div>

    <!-- <div>时间库 {{ moment().format() }}</div> -->

    <!--选择日期时进行限制 -->
    <div class="limit-date">
      <el-date-picker size="small" value-format="yyyy-MM-dd" v-model="value2" type="date" placeholder="选择日期"
        @change="selectDate" :picker-options="pickerOptions"></el-date-picker>
    </div>

    <!-- 默认展示上个月的月初和月末-->
    <div class="limit-date">
      <el-date-picker v-model="value3" type="daterange" range-separator="至" start-placeholder="开始日期"
        end-placeholder="结束日期" value-format="yyyy-MM-dd"></el-date-picker>
    </div>

    <!-- 上个月 -->
    <div class="limit-date">
      <el-date-picker size="small" value-format="yyyy-MM-dd" v-model="value4" type="date" placeholder="选择日期"
        @change="selectDate" :picker-options="pickerOptions"></el-date-picker>
    </div>
    <!-- 上上个月 -->
    <div class="limit-date">
      <el-date-picker size="small" value-format="yyyy-MM-dd" v-model="value5" type="date" placeholder="选择日期"
        @change="selectDate" :picker-options="pickerOptions"></el-date-picker>
    </div>
  </div>
</template>

<script>
import { nowDate, GetDate2 } from '../../../../utils/Date'
import moment from 'moment'

export default {
  data() {
    return {
      time: nowDate("yyyy-MM-dd HH:mm:ss"),
      time1: nowDate("yyyy-MM-dd"),
      time2: nowDate("yyyy年-mm月-dd日"),
      value2: "",
      value3: "",
      value4: "",
      value5: "",
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
          //time.getTime() > Date.now(); 选择今天及今天之前的日期；
          //time.getTime() > Date.now() - 3600 * 1000 * 24;选择今天之前的日期，不包括今天；
          //time.getTime() < Date.now();选择今天之后的日期；
          //time.getTime() < Date.now() - 3600 * 1000 * 24;选择今天及今天之后的日期；
        }
      }
    };
  },
  created() {
    this.getLastMonthDate();
    this.lastMonth();
    this.weekDay();
  },
  methods: {
    //未来一周的日期包括今天
    weekDay() {
      const dateArray = [];
      for (let i = 0; i < 80; i++) {
        const date = new Date(Date.now() + i * 24 * 60 * 60 * 1000);
        const year = date.getFullYear();
        const month =
          date.getMonth() + 1 < 10
            ? `0${date.getMonth() + 1}`
            : date.getMonth() + 1;
        const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
        dateArray.push(`${year}-${month}-${day}`);
      }
      console.log(dateArray)
    },



    getLastMonthDate() {
      let date = new Date();
      let year = date.getFullYear().toString();
      let month =
        date.getMonth() < 10
          ? "0" + date.getMonth().toString()
          : date.getMonth().toString();

      //如果月份是01,上个月就是去年的12月初到12月末
      if (month == "00") {
        year = Number(year) - 1;
        month = "12";
      }
      // var da = date.getDate() < 10 ? '0' + date.getDate().toString() : date.getDate().toString()
      let da = new Date(year, month, 0).getDate();
      da < 10 ? "0" + da.toString() : da.toString();
      let beg = year + "-" + month + "-01";
      let end = year + "-" + month + "-" + da;
      console.log([beg, end]);
      this.value3 = [beg, end]; //将值设置给插件绑定的数据
    },
    lastMonth() {
      let date = new Date();
      let year = date.getFullYear().toString();
      let month =
        date.getMonth() < 10
          ? "0" + date.getMonth().toString()
          : date.getMonth().toString();
      //如果月份是01,上个月就是去年的12月初到12月末
      console.log(month);
      //上个月
      if (month == "00") {
        year = Number(year) - 1;
        month = "12";
      }
      this.value4 = year + "-" + month;
      let tow = new Date(this.value4);
      let month2 =
        tow.getMonth() < 10
          ? "0" + tow.getMonth().toString()
          : tow.getMonth().toString();
      if (month2 == "0") {
        year = Number(year) - 1;
        month2 = "12";
      }
      this.value5 = year + "-" + month2;

      console.log(this.value4);
      console.log(this.value5);
    }
  }
};
</script>


<style>
.limit-date {
  width: 100%;
  height: 100px;
  margin-top: 20px;
  border: 1px solid black;
  padding: 30px;
}
</style>
